<!-- 各类状态弹窗组件 -->
<template>
	<u-popup
		:show="pShow"
		:round="10"
		closeOnClickOverlay
		@close="pShow = false"
		mode="bottom"
		:safeAreaInsetBottom="userType === 0"
	>
		<view>
			<view class="fz-16 fw-6 linh-40 px-15 borderB">切换身份</view>

			<view class="pa-15">
				<view v-for="user in userList" :key="user.id" class="disFlxJS identityBox" @click="changeUserType(user)">
					<view class="disFlxAC">
						<view class="headImg">
							<image
								v-if="user.headimg"
								:src="user.headimg"
								mode="aspectFill"
								style="width: 100%; height: 100%"
							/>
							<u-icon v-else name="account" color="#FFFFFF" size="40"></u-icon>
						</view>
						<view class="pr-10">{{ user.userName }}</view>
						<u-tag :text="user.userType === 0 ? '专家' : '用户'" plain shape="circle" size="mini"></u-tag>
					</view>
          <view>
            <image
              v-if="user.userType === userType"
              src="@/static/image/image220701(14).png"
              mode="aspectFill"
              style="width: 32rpx; height: 32rpx"
            />
          </view>
				</view>
			</view>
		</view>
	</u-popup>
</template>

<script>
import expApi from '@/api/expertIdentity.js'
export default {
	props: {
		show: {
			type: Boolean,
			default: false,
		},
    defaultValue: {
      type: String,
      default: 'user'
    }
	},
	watch: {
		show(nval) {
			this.pShow = nval;
		},
		pShow(nval) {
			if (!nval) {
				this.$emit("close");
			}else{
        this.getInfo();
      }
		},
	},
  computed: {
    userType() {
      return this.$store.state.userType
    },
    userList() {
      return [this.$store.state.userInfo, this.$store.state.expInfo]
    }
  },
	data() {
		return {
			pShow: false
		};
	},
	methods: {
    // 获取身份信息
    async getInfo() {
      if(this.userType === 1){ // 当前为用户,获取专家信息
        // 写死的验证码,此接口为后端开放接口
        const { data } = await expApi.expLogin({ phone: this.userInfo.mobile, smsCode: '000000'});
        this.$store.commit("SET_EXPINFO", data.user || {}); 
      }
    },
    // 切换用户身份
    changeUserType(user){
      this.$store.commit("SET_USERTYPE", user.userType);
      user.userType === 0 ? uni.reLaunch({url: '/expertIdentity/home/index'}) : this.goPage('/pages/my/index');
    }
  },
};
</script>

<style lang="scss" scoped>
.headImg {
	width: 128rpx;
	height: 128rpx;
	border: solid 2px #ffffff;
	border-radius: 50%;
	display: flex;
	align-items: center;
	justify-content: center;
	overflow: hidden;
	margin-right: 32rpx;
	flex-shrink: 0;
}
.identityBox + .identityBox {
	margin-top: 20rpx;
}
</style>
